# from education.settings import SECRET_KEY
import jwt


class Myjwt:

    def __init__(self) -> None:
        self.key = 'django-insecure-esgd4z%kz70pfaj0y=^lx4-!(+6=b@n!163zvnexa=)xdeazo1'

    def jwt_encode(self, data):
        return jwt.encode(data, self.key, algorithm='HS256')

    def jwt_decode(self, token):
        try:
            return jwt.decode(token, self.key, algorithms='HS256')
        except:
            return None

    def jwt_check(self, token):
        try:
            # 解析用户传递token中的数据
            payload = self.jwt_decode(token)
            # 用用户信息和服务器秘钥重新生成token
            newtoken = self.jwt_encode(payload)
            # 对比
            if newtoken == token:
                return True
        except:
            return False


myjwt = Myjwt()
# import time
# data={"data":{"userid":1,'exp':int(time.time())}}
# token = myjwt.jwt_encode(data)
# print(token)
# users = myjwt.jwt_decode("2342423.aa.asd")
# print(users)